package ch.bfh.mobicomp.soda2.gps_datacollector.business.utils;

import android.app.Service;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.google.gson.Gson;

public class SODA_GPS_SharedPreferenceManager {

    private static final String TAG = SODA_GPS_SharedPreferenceManager.class.getName();
    private static SODA_GPS_SharedPreferenceManager preferenceManager = null;

    private static final String GPS_STATE_STARTED = "GPS_STARTED";
    private static final String GPS_STATE_STOPED = "GPS_STOPED";

    private static final String SHAREDPREFERENCE_NAME = "GPS_SHAREDPREFERENCE";

    public static final String GPS_PREFERENCE_STATE_NAME = "GPS_PREFERENCE_STATE";
    public static final String GPS_PREFERENCE_TIMEDELAY_NAME = "GPS_PREFERENCE_TIMEDELAY";
    public static final String GPS_PREFERENCE_DISTDELAY_NAME = "GPS_PREFERENCE_DISTDELAY";
    public static final String GPS_PREFERENCE_FILENAME_NAME = "GPS_PREFERENCE_FILENAME";
    public static final String GPS_PREFERENCE_TIMESTAMP_NAME = "GPS_PREFERENCE_TIMESTAMP";
    public static final String GPS_PREFERENCE_ALTITUDE_NAME = "GPS_PREFERENCE_ALTITUDE";
    public static final String GPS_PREFERENCE_LATITUDE_NAME = "GPS_PREFERENCE_LATITUDE";
    public static final String GPS_PREFERENCE_LONGITUDE_NAME = "GPS_PREFERENCE_LONGITUDE";
    public static final String GPS_PREFERENCE_JSON_NAME = "JSON";

    private static final long DEFAUKT_DELAY_MILLISEC = 1000;
    private static final float DEFAULT_DISTANCE_DIFFERENCE_METER = 0.01f;

    private Editor editor = null;
    private SharedPreferences preferences = null;

    public static SODA_GPS_SharedPreferenceManager getInstance(Context context) {
        if (preferenceManager == null) {
            preferenceManager = new SODA_GPS_SharedPreferenceManager(context);
            return preferenceManager;
        } else {
            return preferenceManager;
        }
    }

    public SODA_GPS_SharedPreferenceManager(Context context) {
        preferences = context.getSharedPreferences(SHAREDPREFERENCE_NAME, Service.MODE_PRIVATE);
        editor = preferences.edit();
        editor.putString(GPS_PREFERENCE_FILENAME_NAME, SHAREDPREFERENCE_NAME);
    }

    public void saveStateStarted() {
        editor.putString(GPS_PREFERENCE_STATE_NAME, GPS_STATE_STARTED);
        editor.commit();
    }

    public void saveStateStoped() {
        editor.putString(GPS_PREFERENCE_STATE_NAME, GPS_STATE_STOPED);
        editor.commit();
    }

    public boolean wasStarted() {
        if (preferences.getString(GPS_PREFERENCE_STATE_NAME, GPS_STATE_STARTED).equals(GPS_STATE_STARTED)) {
            return true;
        }
        return false;
    }

    public void saveMinTimeDelay(long timeDelay) {
        editor.putLong(GPS_PREFERENCE_TIMEDELAY_NAME, timeDelay);
        editor.commit();
    }

    public void saveMinDistanceDelay(float distanceDelay) {
        editor.putFloat(GPS_PREFERENCE_DISTDELAY_NAME, distanceDelay);
        editor.commit();
    }

    public long getMinTimeDelay() {
        return preferences.getLong(GPS_PREFERENCE_TIMEDELAY_NAME, DEFAUKT_DELAY_MILLISEC);

    }

    public float getMinDistanceDelay() {
        return preferences.getFloat(GPS_PREFERENCE_DISTDELAY_NAME, DEFAULT_DISTANCE_DIFFERENCE_METER);

    }

    public String getSharedPreferenceFileName() {
        return preferences.getString(GPS_PREFERENCE_FILENAME_NAME, SHAREDPREFERENCE_NAME);
    }

    public void saveLastDate(String timestamp, String latitude, String longitude, String altitude) {
        editor.putString(GPS_PREFERENCE_ALTITUDE_NAME, altitude);
        editor.putString(GPS_PREFERENCE_TIMESTAMP_NAME, timestamp);
        editor.putString(GPS_PREFERENCE_LONGITUDE_NAME, longitude);
        editor.putString(GPS_PREFERENCE_LATITUDE_NAME, latitude);
        String jsonText = createJsonText(timestamp, latitude, longitude, altitude);
        Log.d(TAG, jsonText);
        editor.putString(GPS_PREFERENCE_JSON_NAME, jsonText);
        editor.commit();
        Log.d(TAG, "Saved Last Position in Shared Preferences");
    }

    private String createJsonText(String timestamp, String latitude, String longitude, String altitude) {
        String json = "GPS|t:" + timestamp + " LA:" + latitude + " LO:" + longitude + " AL:" + altitude;
        Log.d(TAG, json);
        return json;
    }

}
